try:
    import scapy.all as scapy
except ImportError:
    import scapy

try:
    # This import works from the project directory
    import scapy_http.http
except ImportError:
    # If you installed this package via pip, you just need to execute this
    from scapy.layers import http
import csv
import os
import json
import time
import datetime


HOST = []
newUrl = []
total = []
Result = []


def main():
    oldUrl = os.listdir('./status')
    L = os.listdir('./pcapFile')
    o_url = []
    for i in L:
        if i[0] != '.':
            for j in oldUrl:
                p_name = j.split('_')[0]
                o_url.append(p_name)
            if not i in o_url :
                newUrl.append(i)
    startAnalysis(newUrl)

def startAnalysis(newUrl):
    for i in newUrl:
        url = ['./pcapFile', i, 'capture.pcap']
        j = '/'.join(url)
        print j
        portUrl = ['./pcapFile',i,'status.txt']
        port = '/'.join(portUrl)
        f= open(port,"r")
        arr = f.read()
        v_arr = json.loads(arr)

        packets = scapy.rdpcap(j)

        addHost(packets, v_arr)

        build_csv(i,HOST,v_arr)

def addHost(packets, v_arr):
    for p in packets:
        data = []
        #print '======================='

        #print p.time

        time_stamp = p.time
        data.append(time_stamp)

        Url = 'http://'
        for f in p.payload.fields_desc:

            if f.name == 'len':
                ct = scapy.conf.color_theme
                vcol = ct.field_value
                fvalue = p.payload.getfieldval(f.name)
                reprval = f.i2repr(p.payload,fvalue)

                #print reprval
                data.append(reprval)

                for f in p.payload.payload.payload.payload.fields_desc:

                    if f.name == 'Host':

                        ct = scapy.conf.color_theme
                        vcol = ct.field_value
                        fvalue = p.payload.getfieldval(f.name)
                        reprval = f.i2repr(p.payload,fvalue)

                        #if reprval:
                        Url += reprval[2:-1]

                        data.append(Url)
                        #print Url

                    if f.name == 'Method':

                        ct = scapy.conf.color_theme
                        vcol = ct.field_value
                        fvalue = p.payload.getfieldval(f.name)
                        reprval = f.i2repr(p.payload,fvalue)

                        data.append(reprval[1:])
                        #print reprval

        Result.append(data)
        #print Result


    f = v_arr[2]['date']
    s = v_arr[3]['date']

    for idx, j in enumerate(Result):
        data = []
        l = len(j)
        if l == 4:
            #print j
            try:
                size = Result[idx + 1]
            except:
                continue
            pcap_time = time.localtime(j[0])
            st = time.strftime('%H:%M:%S', pcap_time)
            #print st
            if st >= f and st <= s:
                data.append('1')
            elif st > s:
                data.append('2')
            else:
                data.append('0')

            data.append(j[0])
            data.append(j[2])
            data.append(j[3])
            time_difference = size[0] - j[0]
            data.append(time_difference)
            data.append(j[1])
            if len(size) == 2:
                #print size[1]
                data.append(size[1])

        total.append(data)

    for i in total:
        if len(i) != 0:
            HOST.append(i)


def build_csv(apk,Host,v_arr):
    #print Host
    root_path = os.getcwd()
    version = v_arr[4]

    tmp_path = root_path + "/status/" + apk +'_'+ version +".csv"
    f = open(tmp_path, "a+")
    writer = csv.writer(f)
    for j in Host:
            writer.writerow(j)
    f.close()
    del HOST[:]

if __name__ == '__main__':
    main();
